/*
 *    Copyright 2014-2017 Alexey Danilov
 *    Copyright 2021 Institute of Software Chinese Academy of Sciences, ISRC

 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.danikula.videocache;

import com.danikula.videocache.file.DiskUsage;
import com.danikula.videocache.file.FileNameGenerator;
import com.danikula.videocache.headers.HeaderInjector;
import com.danikula.videocache.sourcestorage.SourceInfoStorage;

import java.io.File;

/**
 * Configuration for proxy cache.
 *
 * @author Alexey Danilov (danikula@gmail.com).
 */
public class Config {
    /**
     * 临时缓存文件文件夹
     */
    public final File cacheRoot;
    /**
     * 临时缓存文件重命名工具
     */
    public final FileNameGenerator fileNameGenerator;
    /**
     * 缓存文件操作工具
     */
    public final DiskUsage diskUsage;
    /**
     * 视频信息存储数据库工具
     */
    public final SourceInfoStorage sourceInfoStorage;
    /**
     * 请求头
     */
    public final HeaderInjector headerInjector;
    /**
     * 缓存策略
     */
    public final CacheStrategy cacheStrategy;


    public Config(File cacheRoot, FileNameGenerator fileNameGenerator,
                  DiskUsage diskUsage, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector,
                  CacheStrategy cacheStrategy) {
        this.cacheRoot = cacheRoot;
        this.fileNameGenerator = fileNameGenerator;
        this.diskUsage = diskUsage;
        this.sourceInfoStorage = sourceInfoStorage;
        this.headerInjector = headerInjector;
        this.cacheStrategy = cacheStrategy;
    }

    /**
     * 使用新的MD5加密得到的文件名生成临时缓存文件
     * @param url 视频UR
     * @return 临时缓存文件
     */
    public File generateCacheFile(String url) {
        String name = fileNameGenerator.generate(url);
        return new File(cacheRoot, name);
    }

    /**
     * 对视频URL做MD5加密生成新的文件名
     *
     * @param url 视频UR
     * @return 新的文件名
     */
    public static String generateCacheName(String url) {
        return ProxyCacheUtils.computeMD5(url);
    }
}
